<template>
 <div>
    <!-- 一条文章单元格 -->
  <van-cell @click="$router.push(`/article?art_id=${artObj.art_id}`)">
    <!-- 标题区域的插槽 -->
  <template #title>
   <div class="title-box">
     <!-- 标题 -->
     <span>{{ artObj.title }}</span>
     <!-- 单图 -->
     <!-- v-lazy 图片懒加载指令,替换src -->
     <img class="thumb" v-lazy="artObj.cover.images[0]" v-if="artObj.cover.type == 1"/>
    </div>
      <!-- 三张图片 -->
    <div class="thumb-box" v-if="artObj.cover.type > 1">
    <img class="thumb"
    v-for="(imgUrl, index) in artObj.cover.images"
    :key="index"
    v-lazy="imgUrl"
    />
    </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>作者 {{artObj.aut_name}}</span>
          <span>评论 {{artObj.comm_count}}</span>
          <span>发布日期 {{$time(artObj.pubdate)}}</span>
        </div>
        <!-- 反馈按钮 -->
        <!-- click阻止冒泡 -->
        <van-icon name="cross" @click.stop="show = true" />
      </div>
    </template>
    </van-cell>
   <!-- 反馈面板 -->
    <van-action-sheet
    v-model="show"
    :actions="actions"
    get-container="body"
    :cancel-text="cancelText"
    @select="onSelect"
    @cancel="onCancel"/>
  <!-- 其它问题弹出消息输入内容框 -->
  <van-dialog
  v-model="isShow"
  title="其它问题"
  show-cancel-button
  get-container="body"
  :before-close="onBeforeClose">
    <van-field type="textarea" placeholder="请输入内容" v-model="msg" ref="box" v-focus></van-field>
  </van-dialog>
 </div>

</template>

<script>
import { firstActions, secondActions } from '@/api/report'
import { artDislikeAPI, artReportAPI } from '@/api/home'
export default {
  name: 'ArticleItem',
  props: {
    artObj: {
      type: Object,
      required: true
    },
    i: Number,
    showCross: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      show: false,
      actions: firstActions,
      cancelText: '取消', // 底部按钮文字
      isShow: false, // 反馈其它问题弹出层默认关闭
      msg: '' // 反馈内容默认为空
    }
  },
  methods: {
    // 二级面板反馈其它问题
    async onBeforeClose (action, done) {
      if (action === 'cancel') return done()
      if (!this.msg) {
        done(false)
        this.$toast.fail('请输入反馈内容!')
        return
      }
      try {
        await artReportAPI({
          artId: this.artObj.art_id,
          type: '0',
          remark: this.msg
        })
        this.$toast.success('反馈成功!')
      } catch (e) {
        this.$toast.fail('反馈出错!')
      } finally {
        // 反馈成功或失败均关闭弹窗
        done()
        this.msg = ''
      }
    },
    async onSelect ({ id, name }) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      if (id === 22) {
        this.actions = secondActions
        this.cancelText = '返回' // 修改底部按钮显示文字
      } else if (id === 11) {
        try {
          await artDislikeAPI({ artId: this.artObj.art_id })
          this.show = false
          this.$toast.success('操作成功!')
          // 列表删除文章
          this.$emit('delArt', this.i)
        } catch (error) {
          this.$toast.fail('操作失败!')
        }
      } else if (id === 33) {

      } else {
        if (id === 0) { // 其它问题
          this.show = false
          this.isShow = true
        } else { // 正常问题
          // console.log(id)
          try {
            await artReportAPI({
              artId: this.artObj.art_id,
              type: id + '',
              remark: name
            })
            this.$toast.success('举报成功')
          } catch (e) {
            this.$toast.fail('举报出错')
          } finally {
            this.actions = firstActions
            this.show = false
          }
        }
      }
    },
    // 取消事件
    onCancel () {
      if (this.cancelText === '返回') {
        this.actions = firstActions
        this.cancelText = '取消'
        this.show = true // 点返回不要让弹窗关闭, 点取消文字按钮, 弹窗默认关闭
      }
    }

  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span{
  margin: 0 3px;
  &:first-child{
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
